﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace HYRElectric.PowerSystem
{
	internal class Utility
	{
		public static string GetEquipWorksetName(FamilyInstance fiEquip)
		{
			Workset workset = fiEquip.Document.GetWorksetTable().GetWorkset(fiEquip.WorksetId);
            if (workset.Kind == (WorksetKind)4)
			{
				return workset.Name;
			}
			return "无归属工作集";
		}

		public static IDictionary<string, List<FamilyInstance>> GetEquipWorksetInfoMap(List<FamilyInstance> lstEquips)
		{
			IDictionary<string, List<FamilyInstance>> dictionary = new Dictionary<string, List<FamilyInstance>>();
			foreach (FamilyInstance familyInstance in lstEquips)
			{
				string equipWorksetName = Utility.GetEquipWorksetName(familyInstance);
				if (dictionary.ContainsKey(equipWorksetName))
				{
					dictionary[equipWorksetName].Add(familyInstance);
				}
				else
				{
					dictionary.Add(equipWorksetName, new List<FamilyInstance>
					{
						familyInstance
					});
				}
			}
			return dictionary;
		}

		public static void UpdateEquipInTreeView(TreeNode treeNode, List<FamilyInstance> lstEquipsLevelAll)
		{
			foreach (KeyValuePair<string, List<FamilyInstance>> keyValuePair in Utility.GetEquipWorksetInfoMap(lstEquipsLevelAll))
			{
				TreeNode treeNode2 = new TreeNode(keyValuePair.Key);
				treeNode.Nodes.Add(treeNode2);
				List<FamilyInstance> value = keyValuePair.Value;
                //IEnumerator<string> enumerator2 = (from e in value;
                //select e.Symbol.Family.FamilyCategory.Name).Distinct<string>().GetEnumerator())
                //{
                //    while (enumerator2.MoveNext())
                //    {
                //        string strCategoryName = enumerator2.Current;
                //        TreeNode treeNode3 = new TreeNode(strCategoryName);
                //        treeNode2.Nodes.Add(treeNode3);
                //        treeNode3.Tag = strCategoryName;
                //        int num = 0;
                //        IEnumerator<string> enumerator3 = (from e in value;
                //        where e.Symbol.Family.FamilyCategory.Name == strCategoryName
                //        select e.Symbol.Family.Name).Distinct<string>().GetEnumerator())
                //        {
                //            while (enumerator3.MoveNext())
                //            {
                //                string strFamilyName = enumerator3.Current;
                //                TreeNode treeNode4 = new TreeNode(strFamilyName);
                //                treeNode3.Nodes.Add(treeNode4);
                //                treeNode4.Tag = strFamilyName;
                //                IEnumerator<string> enumerator4 = (from e in valu;
                //                where e.Symbol.Family.FamilyCategory.Name == strCategoryName && e.Symbol.Family.Name == strFamilyName
                //                select e.Symbol.Name).Distinct<string>().GetEnumerator())
                //                {
                //                    while (enumerator4.MoveNext())
                //                    {
                //                        string strSymbolName = enumerator4.Current;
                //                        TreeNode treeNode5 = new TreeNode(strSymbolName);
                //                        treeNode4.Nodes.Add(treeNode5);
                //                        foreach (FamilyInstance familyInstance in from e in value
                //                        where e.Symbol.Family.FamilyCategory.Name == strCategoryName && e.Symbol.Family.Name == strFamilyName && e.Symbol.Name == strSymbolName
                //                        select e)
                //                        {
                //                            TreeNode treeNode6 = new TreeNode(familyInstance.GetFamilyInstanceName());
                //                            treeNode5.Nodes.Add(treeNode6);
                //                            treeNode6.Tag = familyInstance;
                //                            num++;
                //                        }
                //                    }
                //                }
                //            }
                //        }
                //        treeNode3.Text = string.Format("{0} (共计：{1}个)", treeNode3.Text, num);
                //    }
                //}
			}
		}
	}
}
